package com.ruoyi.jproom.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.jproom.entity.*;
import com.ruoyi.jproom.service.HousingEntrustService;
import com.ruoyi.jproom.service.ResidentialService;
import com.ruoyi.jproom.service.SellService;
import com.ruoyi.jproom.utils.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;


/**
 * @author zeo
 * @explain 房源委托控制层
 * @date 2020/7/27 11:13
 */
@Controller
@RequestMapping("/housingEntrust")
public class HousingEntrustController extends BaseController {

    @Autowired
    HousingEntrustService housingService;

    @Autowired
    ResidentialService residentialService;

    @Autowired
    SellService sellService;

    /**
     * 客户端有房出售委托
     * @param meSellInfo
     * @return
     */
    @RequestMapping("/insertMeSellEntrust")
    @ResponseBody
    public BaseResult insertMeSellEntrust(MeSellInfo meSellInfo){
        return housingService.insertMeSellEntrust(meSellInfo);
    }

    /**
     * 客户端有房出租委托
     * @param meHaveRentInfo
     * @return
     */
    @RequestMapping("/insertMeHaveRentEntrust")
    @ResponseBody
    public BaseResult insertMeHaveRentEntrust(MeHaveRentInfo meHaveRentInfo){
        return housingService.insertMeHaveRentEntrust(meHaveRentInfo);
    }

    /**
     * 获取所有委托信息
     * @param sell
     * @return
     */
    @RequestMapping("/queryEntrustInfoList")
    @ResponseBody
    public List<Map<String,Object>> queryEntrustInfoList(Sell sell){
        return housingService.queryEntrustInfoList(sell);
    }


    /**
     * 服务端有房出售管理跳转
     */
    @GetMapping("/MeSellEntrustManage")
    public String toMeSellEntrust(){
        return "housingentrust/meSellEntrustManage";
    }

    /**
     * 服务端获取用户委托的有房出售委托数据
     * @return
     */
    @RequestMapping("/findMeSellEntrustList")
    @ResponseBody
    public TableDataInfo findMeSellEntrustList() {
        startPage();
        return getDataTable(housingService.findMeSellEntrustList());
    }

    /**
     * 服务端处理出售委托跳转
     * @param id
     * @param mmap
     * @return
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap){
        MeSellInfo meSellInfo = housingService.findMeSellEntrustById(id);
        mmap.put("meSellInfo",meSellInfo);
        if(meSellInfo.getResidentialId() != null){

            //使用委托id取房源信息,先判断房源是否生成,已生成则代表已处理
            String housingId = housingService.findHousingIdMeSellEntrustId(id);
            if(housingId != null){
                Housing housing = housingService.findHousingById(housingId);
                mmap.put("housing",housing);
                //再拿小区id找到售卖信息获取标题以及标签
                Sell sell = sellService.findSellInfoByHousingId(housingId);
                mmap.put("sell",sell);
            }
            //取小区信息
            Residential residential = residentialService.findResidentByResidentialId(meSellInfo.getResidentialId());
            mmap.put("residential",residential);
        }
        return "housingentrust/meSellEntrustHandle";
    }

    /**
     * 服务端处理房源出售
     * @param residentialFile 小区图片
     * @param residentialId 小区id
     * @param userId 用户唯一标识
     * @param isResidential 小区是否是新增
     * @param residential 小区对象
     * @param housingFile 房源图片
     * @return
     */
    @RequestMapping("/addHousingSell")
    @ResponseBody
    public BaseResult addHousingSell(@RequestParam(value = "residentialFile") MultipartFile[] residentialFile, String residentialId, String userId, int isResidential, Residential residential, Housing housing, @RequestParam(value = "housingFile") MultipartFile[] housingFile, Sell sell) throws IOException {
        return housingService.addHousingSell(residentialFile,residentialId,userId,isResidential,residential,housing,housingFile,sell);
    }



    /**
     * 服务端有房出租管理跳转
     */
    @GetMapping("/MeHaveRentEntrustManage")
    public String MeHaveRentEntrustManage(){
        return "housingentrust/meHaveRentEntrustManage";
    }

    /**
     * 服务端获取用户委托的有房出租委托数据
     * @return
     */
    @RequestMapping("/findMeHaveRentEntrustList")
    @ResponseBody
    public TableDataInfo findMeHaveRentEntrustList() {
        startPage();
        return getDataTable(housingService.findMeHaveRentEntrustList());
    }

    /**
     * 服务端处理租房委托跳转
     * @param id
     * @param mmap
     * @return
     */
    @GetMapping("/meHaveRentEdit/{id}")
    public String meHaveRentEdit(@PathVariable("id") String id, ModelMap mmap){
        MeHaveRentInfo meHaveRentInfo = housingService.findMeHaveRentInfoEntrustById(id);
        mmap.put("meHaveRentInfo",meHaveRentInfo);
        if(meHaveRentInfo.getResidentialId() != null){

            //使用委托id取房源信息,先判断房源是否生成,已生成则代表已处理
            String housingId = housingService.findHousingIdByMeHaveRentEntrustId(id);
            if(housingId != null){
                Housing housing = housingService.findHousingById(housingId);
                mmap.put("housing",housing);
                //再拿小区id找到售卖信息获取标题以及标签
                Sell sell = sellService.findSellInfoByHousingId(housingId);
                mmap.put("sell",sell);
            }

            //取小区信息
            Residential residential = residentialService.findResidentByResidentialId(meHaveRentInfo.getResidentialId());
            mmap.put("residential",residential);
        }
        return "housingentrust/meHaveRentEntrustHandle";
    }

    /**
     * 服务端处理房源出租
     * @param residentialFile 小区图片
     * @param residentialId 小区id
     * @param userId 用户唯一标识
     * @param isResidential 小区是否是新增
     * @param residential 小区对象
     * @param housingFile 房源图片
     * @return
     */
    @RequestMapping("/addHousingRent")
    @ResponseBody
    public BaseResult addHousingRent(@RequestParam(value = "residentialFile") MultipartFile[] residentialFile, String residentialId, String userId, int isResidential, Residential residential, Housing housing, @RequestParam(value = "housingFile") MultipartFile[] housingFile, Sell sell) throws IOException {
        return housingService.addHousingRent(residentialFile,residentialId,userId,isResidential,residential,housing,housingFile,sell);
    }






}
